/**
 * @author YinHang
 */
;(function () {
	//
	function Node(data, next, prev) {
		this.data = data;
		this.next = next;
		this.prev = prev;
	};
	
	//
	function Iterator(container) {
		this.container = container;
		this.curIndex = container.head;
	};
	
	Iterator.prototype.hasNext = function () {
		return this.curIndex ? true : false;
	};
	
	Iterator.prototype.next = function () {
		if(this.curIndex)
		{
			var curData = this.curIndex.data;
			var next = this.curIndex.next;
			if(next)
				this.curIndex = next;
			else
			{
				this.curIndex = null;
			}
			return curData;
		}
		return null;
	};
	
	Iterator.prototype.nextEntity = function () {
		if(this.curIndex)
		{
			var curData = this.curIndex;
			var next = this.curIndex.next;
			if(next)
				this.curIndex = next;
			else
			{
				this.curIndex = null;
			}
			return curData;
		}
		return null;
	};
	
	//链表
	function LinkedList() {
		this.head = null;
		this.last = null;
		this.size = 0;
	};
	
	var Prototype = LinkedList.prototype;
	
	Prototype.add = function (data) {
		if(this.head)
		{
			if(this.last)
			{
				var newNode = new Node(data, null, null);
				this.last.next = newNode;
				newNode.prev = this.last;
				this.last = newNode;
			}
			else
			{
				var newNode = new Node(data, null, null);
				this.last = newNode;
				this.last.prev = this.head;
				this.head.next = this.last;
			}
		}
		else
		{
			this.head = new Node(data, null, null);
		}
		++ this.size;
		return this.last || this.head;
	};
	
	Prototype.remove = function (nodeEntity) {
		this.link(nodeEntity.prev, nodeEntity.next);
		-- this.size;
	};
	
	Prototype.link = function (node1, node2) {
		if(node1)
		{
			if(node2)
			{
				node1.next = node2;
				node2.prev = node1;
			}
			else
			{
				this.head = node1;
				node1.prev = null;
				node1.next = null;
			}
		}
		else if(node2) {
			this.head = node2;
			node2.prev = null;
		}
		else
		{
			this.head = null;
			this.last = null;
		}
	};
	
	Prototype.iterator = function () {
		return new Iterator(this);
	};
	
	window.Iterator = Iterator;
	window.LinkedList = LinkedList;
})();
